/**
 * 
 */
package com.dharma.mydiet.view;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import org.achartengine.GraphicalView;
import org.achartengine.model.CategorySeries;
import org.achartengine.model.TimeSeries;

import android.app.ActionBar;
import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;

import com.dharma.mydiet.R;
import com.dharma.mydiet.storage.DAOFood;
import com.dharma.mydiet.storage.DAORegister;
import com.dharma.mydiet.storage.DAORegisterFood;
import com.dharma.mydiet.storage.DAORegisterPersonal;
import com.dharma.mydiet.storage.TransactionalFood;
import com.dharma.mydiet.storage.TransactionalRegister;
import com.dharma.mydiet.storage.TransactionalRegisterFood;
import com.dharma.mydiet.storage.TransactionalRegisterPersonal;
import com.dharma.mydiet.utility.Utils;
import com.google.analytics.tracking.android.EasyTracker;

/**
 * @author josh
 * 
 */

public class GraphicosQualityActivity extends Activity {

    private ArrayList<TransactionalRegister> registers;
    private ArrayAdapter<CharSequence> adapterPeriodos;
    private Spinner periodos;
    private CategorySeries serie;

    protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	this.setContentView(R.layout.activity_graphic_quality);
	int title = Resources.getSystem().getIdentifier("action_bar_title", "id", "android");
	Utils.applyCustomFont((TextView) findViewById(title), getApplicationContext());
	ActionBar ab = getActionBar();
	ab.setSubtitle("Graficos de Registros");
	
	periodos = (Spinner) findViewById(R.id.periodo);
	adapterPeriodos = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item);
	adapterPeriodos.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	periodos.setAdapter(adapterPeriodos);
	adapterPeriodos.add("Ultimo dia");
	adapterPeriodos.add("Ultima semana");
	adapterPeriodos.add("Ultimo mes");
	adapterPeriodos.add("Todos los tiempos");
	
	serie = new CategorySeries("Calidad de Alimentacion");
	periodos.setOnItemSelectedListener(new OnItemSelectedListener() {

	    private float totalProteinas;
	    private float totolLipidos;
	    private float tatalCarbohidratos;

	    @Override
	    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		ArrayList registers = DAORegisterFood.getInstance().getUsers();
		if (registers != null && registers.size() > 0) {
		    serie = new CategorySeries("Calidad de Alimentacion");
		    for (int i = 0; i < registers.size(); i++) {
			TransactionalRegisterFood register = (TransactionalRegisterFood) registers.get(i);
			TransactionalFood food = DAOFood.getInstance().getFood("" + register.getIdFoodValue());

			Date today = Calendar.getInstance().getTime();
			long startTime = (register.getDate()).getTime();
			long endTime = today.getTime();
			long diffTime = endTime - startTime;
			long diffDays = diffTime / (1000 * 60 * 60 * 24);

			switch (arg2) {
			case 0:
			    if (diffDays <= 1) {
				totalProteinas += ((register.getQuantity() / 100) * food.getPorcentualProteins());
				totolLipidos += ((register.getQuantity() / 100) * food.getPorcentualLipids());
				tatalCarbohidratos += ((register.getQuantity() / 100) * food
					.getPorcentualCarbohidraths());
			    }
			    break;
			case 1:
			    if (diffDays <= 7) {
				totalProteinas += ((register.getQuantity() / 100) * food.getPorcentualProteins());
				totolLipidos += ((register.getQuantity() / 100) * food.getPorcentualLipids());
				tatalCarbohidratos += ((register.getQuantity() / 100) * food
					.getPorcentualCarbohidraths());
			    }
			    break;
			case 2:
			    if (diffDays <= 31) {
				totalProteinas += ((register.getQuantity() / 100) * food.getPorcentualProteins());
				totolLipidos += ((register.getQuantity() / 100) * food.getPorcentualLipids());
				tatalCarbohidratos += ((register.getQuantity() / 100) * food
					.getPorcentualCarbohidraths());
			    }
			    break;
			case 3:
			    totalProteinas += ((register.getQuantity() / 100) * food.getPorcentualProteins());
			    totolLipidos += ((register.getQuantity() / 100) * food.getPorcentualLipids());
			    tatalCarbohidratos += ((register.getQuantity() / 100) * food.getPorcentualCarbohidraths());
			    break;
			default:
			    totalProteinas += ((register.getQuantity() / 100) * food.getPorcentualProteins());
			    totolLipidos += ((register.getQuantity() / 100) * food.getPorcentualLipids());
			    tatalCarbohidratos += ((register.getQuantity() / 100) * food.getPorcentualCarbohidraths());
			    break;
			}
		    }
		}
		serie.add("Proteinas", totalProteinas);
		serie.add("Lipidos", totolLipidos);
		serie.add("Carbohidratos", tatalCarbohidratos);
		drawGraphic(serie);
	    }

	    @Override
	    public void onNothingSelected(AdapterView<?> arg0) {

	    }
	});

    }

    private void drawGraphic(CategorySeries serie) {
	GraphicQuality graph = new GraphicQuality();
	GraphicalView gr = graph.getView(this, serie);
	LinearLayout layout = (LinearLayout) findViewById(R.id.charts);
	layout.removeAllViews();
	layout.addView(gr);

    }

    @Override
    public void onStart() {
	super.onStart();
	EasyTracker.getInstance(this).activityStart(this); // Add this method.
    }

    @Override
    public void onStop() {
	super.onStop();
	EasyTracker.getInstance(this).activityStop(this); // Add this method.
    }
}
